<?php
session_start();
require_once ("Login/config.php");
require_once 'Login/databaseconnection.php';
if(!loggedin())
{
	require_once ("index.php");
}
else
{
if($_GET['action']=="single")
{
$param=str_replace("'","",$_GET['q']);
$array= explode(",",$param);
$m=count($array);
$table=$array[$m-1];
		 $Q="SELECT * FROM ".$array[$m-1]." WHERE ".str_replace(" ","_",$array[$m-2]);
		 for($i=0;$i<$m-2;$i++)
		 {
		  if($i==0)
		 {
		 $Q=$Q." IN (";
		 }		 
		 $Q=$Q."'".$array[$i]."'";
		 if($i+1<$m-2)
		 {
		 $Q=$Q.",";
		 }
		 if($i==$m-3)
		 {
		 $Q=$Q.")";
		 }
		 }
}
if($_GET['action']=="double")
{
	$param1=$_GET['p'];
	$param2=$_GET['q'];
	$array1= explode(",",$param1);
	$array2= explode(",",$param2);
	$x=count($array1);
	$y=count($array2);	
$table=$array2[$y-1];
 $C="";
		 $Q="SELECT * FROM ".$array2[$y-1]." WHERE ".str_replace(" ","_",$array1[$x-1]);
		 for($i=0;$i<$x-1;$i++)
		 {
		 if($i==0)
		 {
		 $Q=$Q." IN (";
		 $C=$C." IN (";
		 }
		 $Q=$Q."'".$array1[$i]."'";
		  $C=$C."'".$array1[$i]."'";
		 if($i+1<$x-1)
		 {
		 $Q=$Q.",";
		 $C=$C.",";
		 }
		 if($i==$x-2)
		 {
		 $Q=$Q.")";
		 $C=$C.")";
		 }
		 }
		 
		 if($array2[$y-2]=="Winned Times")
		 {
		 $w_t=mysql_query("SELECT * FROM competition WHERE Result='Win' AND School_Id".$C);
		 $num_row=mysql_num_rows($w_t);
		 $j=0;
		 $cou_res=0;
		 while($r_w_t=mysql_fetch_array($w_t))
		 {
		 $cou_res++;
		 $com_id=$r_w_t['Competition_Id'];
		 if($j==0)
		 {
		 $Q=$Q." AND Competition_Id IN (";
		 }
		 $Q=$Q."'".$com_id."'";
		 if($j+1<$num_row)
		 {
		 $Q=$Q.",";
		 }
		 $j++;
		 }
		 if($j==$num_row)
		 {
		 $Q=$Q.")";
		 }
		 }
		 else
		 {
		 for($j=0;$j<$y-2;$j++)
		 {
		 if($j==0)
		 {
		 $Q=$Q." AND ".str_replace(" ","_",$array2[$y-2])." IN (";
		 }
		 $Q=$Q."'".$array2[$j]."'";
		 if($j+1<$y-2)
		 {
		 $Q=$Q.",";
		 }
		 if($j==$y-3)
		 {
		 $Q=$Q.")";
		 }
		 }
		 }
}
if($_GET['action']=="triple")
{
	$param1=$_GET['p'];
	$param2=$_GET['q'];
	$param3=$_GET['r'];
	$array1= explode(",",$param1);
	$array2= explode(",",$param3);
	$array3= explode(",",$param2);
	$x=count($array1);
	$y=count($array2);	
	$z=count($array3);
$table=$array2[$y-1];
$Q="SELECT * FROM ".$array2[$y-1]." WHERE ".str_replace(" ","_",$array1[$x-1]);
		 for($i=0;$i<$x-1;$i++)
		 {
		 if($i==0)
		 {
		 $Q=$Q." IN (";
		 }
		 $Q=$Q."'".$array1[$i]."'";
		 if($i+1<$x-1)
		 {
		 $Q=$Q.",";
		 }
		 if($i==$x-2)
		 {
		 $Q=$Q.")";
		 }
		 }
		 
		 for($j=0;$j<$y-2;$j++)
		 {
		 if($j==0)
		 {
		 $Q=$Q." AND ".str_replace(" ","_",$array2[$y-2])." IN (";
		 }
		 $Q=$Q."'".$array2[$j]."'";
		 if($j+1<$y-2)
		 {
		 $Q=$Q.",";
		 }
		 if($j==$y-3)
		 {
		 $Q=$Q.")";
		 }
		 }
		 for($k=0;$k<$z-1;$k++)
		 {
		 if($k==0)
		 {
		 $Q=$Q." AND ".str_replace(" ","_",$array3[$z-1])." IN (";
		 }
		 $Q=$Q."'".$array3[$k]."'";
		 if($k+1<$z-1)
		 {
		 $Q=$Q.",";
		 }
		 if($k==$z-2)
		 {
		 $Q=$Q.")";
		 }
		 }
}
$que=mysql_query($Q);
if($m>0||$x>0)
{
	  echo '<br/>';
	  
	  if($table=="teacher"||$table=="lab"||$table=="school")
	  {
	  if(mysql_num_rows($que)>10)
	  {
	  echo '<div style="width:100%; height:450px; overflow-x:scroll; overflow-y:scroll;">';
	  }
	  else
	  {
	  echo '<div style="width:100%; overflow-x:scroll;">';
	  }
	  }
	  else
	  {
	  if(mysql_num_rows($que)>10)
	  {
	   echo '<div style="height:450px; overflow-y:scroll;">';
	  }
	  }
	  if($_GET['action']=="single")
	  {	  
	 echo '<table border="1" class="tablefontv">';
	echo '<tr bgcolor="#6AB5FF"><td>';
	if($array[$m-2]=="School Id"&&$array[$m-1]!="school")
		 {
		 echo 'School Name';
		 }
		 else if($array[$m-2]=="Lab Id"&&$array[$m-1]!="lab")
		 {
		 echo 'Lab Type';
		 }
		 else if($array[$m-2]=="Resource Id")
		 {
		 echo 'Resource Type';
		 }
		 else if($array[$m-2]=="Programme Id")
		 {
		 echo 'Programme Name';
		 }
		 else if($array[$m-2]=="Competition Id")
		 {
		 echo 'Competition Name';
		 }
		 else
		 {
		 echo $array[$m-2];
		 }
	echo '</td>';
	if($array[$m-1]=="training_details")
		 {
		 echo "<td>Teacher's name</td>";
		 }
	$cols=mysql_query("SHOW COLUMNS FROM ".$array[$m-1]);
	while($rows=mysql_fetch_row($cols))
		 {
		 if($array[$m-2]!=str_replace("_"," ",$rows[0])&&$rows[0]!="Last_Updated_Date")
		 {
		 if($rows[0]=="School_Id"&&$array[$m-1]!="school")
		 {
		 echo '<td>School Name</td>';
		 }
		 else if($rows[0]=="Lab_Id"&&$array[$m-1]!="lab")
		 {
		 echo '<td>Lab Type</td>';
		 }
		 else if($rows[0]=="Resource_Id")
		 {
		 echo '<td>Resource Type</td>';
		 }
		 else if($rows[0]=="Programme_Id")
		 {
		 echo '<td>Programme Name</td>';
		 }
		 else if($rows[0]=="Competition_Id")
		 {
		 echo '<td>Competition Name</td>';
		 }
		 else if($rows[0]=="Training_Id")
		 {
		 echo '<td>Training Name</td>';
		 }
		 else
		 {
		 echo '<td>'.str_replace("_"," ",$rows[0]).'</td>';
		 }
		 }
		 }
		 echo '</tr>';

		 $que=mysql_query($Q);
		  while($row=mysql_fetch_array($que))
		 {
		 echo '<tr bgcolor="#999999"><td>';
		 if($array[$m-2]=="School Id"&&$array[$m-1]!="School")
		 {
		 $res_que=mysql_query("SELECT * FROM school WHERE School_Id='".$row['School_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array[$m-2]=="Lab Id"&&$array[$m-1]!="lab")
		 {
		 $res_que=mysql_query("SELECT * FROM lab WHERE Lab_Id='".$row['Lab_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Lab_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array[$m-2]=="Resource Id")
		 {
		 $res_que=mysql_query("SELECT * FROM resource WHERE Resource_Id='".$row['Resource_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Resource_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array[$m-2]=="Programme Id")
		 {
		$res_que=mysql_query("SELECT * FROM programme WHERE Programme_Id='".$row['Programme_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Programme_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array[$m-2]=="Competition Id")
		 {		
		 $res_que=mysql_query("SELECT * FROM competitions WHERE Competition_Id='".$row['Competition_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Competition_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else
		 {
		 $col=str_replace(" ","_",$array[$m-2]);
		 $val=$row[$col];
		 }		 
		 echo $val.'</td>';
         if($array[$m-1]=="training_details")
		 {
		 $t_name=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='".$val."'");
		 if($r_t_name=mysql_fetch_array($t_name))
		 {
$name=$r_t_name['First_Name']." ".$r_t_name['Last_Name'];
		 }
		 echo "<td>".$name."</td>";
		 }
		 $cols=mysql_query("SHOW COLUMNS FROM ".$array[$m-1]);
		while($rows=mysql_fetch_row($cols))
		 {
		 if($array[$m-2]!=str_replace("_"," ",$rows[0])&&$rows[0]!="Last_Updated_Date")
		 {
		 if($rows[0]=="School_Id"&&$array[$m-1]!="School")
		 {
		 $res_que=mysql_query("SELECT * FROM school WHERE School_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($rows[0]=="Lab_Id"&&$array[$m-1]!="lab")
		 {
		 $res_que=mysql_query("SELECT * FROM lab WHERE Lab_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Lab_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($rows[0]=="Resource_Id")
		 {
		 $res_que=mysql_query("SELECT * FROM resource WHERE Resource_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Resource_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($rows[0]=="Programme_Id")
		 {
		$res_que=mysql_query("SELECT * FROM programme WHERE Programme_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Programme_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($rows[0]=="Competition_Id")
		 {		
		 $res_que=mysql_query("SELECT * FROM competitions WHERE Competition_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Competition_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($rows[0]=="Training_Id")
		 {		
		 $res_que=mysql_query("SELECT * FROM training WHERE Training_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Training_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else
		 {
		 $val=$row[$rows[0]];
		 }		 
		 echo '<td>'.str_replace("deg_","",$val).'</td>';         
		 }
		 }
		 echo '</tr>';
		 }	
		  echo '</table><br/><br/><br/>';
		}
		
		if($_GET['action']=="double")
	  {
	  echo '<table border="1" class="tablefontv">';
	echo '<tr bgcolor="#6AB5FF"><td>';
	if($array1[$x-1]=="School Id"&&$array2[$y-1]!="school")
		 {
		 echo 'School Name';
		 }
		 else if($array1[$x-1]=="Lab Id"&&$array2[$y-1]!="lab")
		 {
		 echo 'Lab Type';
		 }
		 else if($array1[$x-1]=="Resource Id")
		 {
		 echo 'Resource Type';
		 }
		 else if($array1[$x-1]=="Programme Id")
		 {
		 echo 'Programme Name';
		 }
		 else if($array1[$x-1]=="Competition Id")
		 {
		 echo 'Competition Name';
		 }
		else if($table=="training_details"&&$array1[$x-1]=="Teacher NIC")
		 {
		 echo "Teacher's Name";
		 }
		 else
		 {
	      echo $array1[$x-1];
	     }
	echo '</td><td>';
	    if($array2[$y-2]=="School Id"&&$array2[$y-1]!="school")
		 {
		 echo 'School Name';
		 }
		 else if($array2[$y-2]=="Lab Id"&&$array2[$y-1]!="lab")
		 {
		 echo 'Lab Type';
		 }
		 else if($array2[$y-2]=="Resource Id")
		 {
		 echo 'Resource Type';
		 }
		 else if($array2[$y-2]=="Programme Id")
		 {
		 echo 'Programme Name';
		 }
		 else if($array2[$y-2]=="Competition Id")
		 {
		 echo 'Competition Name';
		 }
		 else if($table=="training_details"&&$array2[$y-2]=="Teacher NIC")
		 {
		 echo "Teacher's Name";
		 }
		 else
		 {
	     echo $array2[$y-2];
	     }
	echo '</td>';
	$cols=mysql_query("SHOW COLUMNS FROM ".$array2[$y-1]);
	while($rows=mysql_fetch_row($cols))
		 {
		 if($array2[$y-2]!=str_replace("_"," ",$rows[0])&&$array1[$x-1]!=str_replace("_"," ",$rows[0])&&$rows[0]!="Last_Updated_Date")
		 {
		 if($rows[0]=="School_Id"&&$array2[$y-1]!="school")
		 {
		 echo '<td>School Name</td>';
		 }
		 else if($rows[0]=="Lab_Id"&&$array2[$y-1]!="lab")
		 {
		 echo '<td>Lab Type</td>';
		 }
		 else if($rows[0]=="Resource_Id")
		 {
		 echo '<td>Resource Type</td>';
		 }
		 else if($rows[0]=="Programme_Id")
		 {
		 echo '<td>Programme Name</td>';
		 }
		 else if($rows[0]=="Competition_Id")
		 {
		 echo '<td>Competition Name</td>';
		 }
		 else
		 {
		 echo '<td>'.str_replace("_"," ",$rows[0]).'</td>';
		 }
		 }
		 }
		 echo '</tr>';
		
		 $que=mysql_query($Q);
		  while($row=mysql_fetch_array($que))
		 {
		 echo '<tr bgcolor="#999999"><td>';
		 if($array2[$x-2]=="Winned Times")
		 {
		 $val=$cou_res;
		 }
		 else if($array1[$x-1]=="School Id"&&$array2[$y-1]!="School")
		 {
		 $res_que=mysql_query("SELECT * FROM school WHERE School_Id='".$row['School_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array1[$x-1]=="Lab Id"&&$array2[$y-1]!="lab")
		 {		 
		 $res_que=mysql_query("SELECT * FROM lab WHERE Lab_Id='".$row['Lab_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		$val=$row_res['Lab_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array1[$x-1]=="Resource Id")
		 {
		 $res_que=mysql_query("SELECT * FROM resource WHERE Resource_Id='".$row['Resource_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Resource_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array1[$x-1]=="Programme Id")
		 {
		$res_que=mysql_query("SELECT * FROM programme WHERE Programme_Id='".$row['Programme_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Programme_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array1[$x-1]=="Competition Id")
		 {		
		 $res_que=mysql_query("SELECT * FROM competitions WHERE Competition_Id='".$row['Competition_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Competition_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($table=="training_details"&&$array1[$x-1]=="Teacher NIC")
		 {
		 $res_que=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='".$row['Teacher_NIC']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['First_Name']." ".$row_res['Last_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else
		 {
		 $col=str_replace(" ","_",$array1[$x-1]);
		 $val=$row[$col];
		 }
		
		 echo $val;
		 echo '</td><td>';		 
		 if($array2[$y-2]=="Winned Times")
		 {
		 $val=$cou_res;
		 }
		 else if($array2[$y-2]=="School Id"&&$array2[$y-1]!="School")
		 {
		 $res_que=mysql_query("SELECT * FROM school WHERE School_Id='".$row['School_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array2[$y-2]=="Lab Id"&&$array2[$y-1]!="lab")
		 {
		 $res_que=mysql_query("SELECT * FROM lab WHERE Lab_Id='".$row['Lab_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Lab_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array2[$y-2]=="Resource Id")
		 {
		 $res_que=mysql_query("SELECT * FROM resource WHERE Resource_Id='".$row['Resource_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Resource_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array2[$y-2]=="Programme Id")
		 {
		$res_que=mysql_query("SELECT * FROM programme WHERE Programme_Id='".$row['Programme_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Programme_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array2[$y-2]=="Competition Id")
		 {		
		 $res_que=mysql_query("SELECT * FROM competitions WHERE Competition_Id='".$row['Competition_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Competition_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($table=="training_details"&&$array2[$y-2]=="Teacher NIC")
		 {
		 $res_que=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='".$row['Teacher_NIC']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['First_Name']." ".$row_res['Last_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else
		 {
		 $col=str_replace(" ","_",$array2[$y-2]);
		 $val=$row[$col];
		 }
		 
		echo str_replace("deg_","",$val);
		 
		 echo '</td>';
		 
		$cols=mysql_query("SHOW COLUMNS FROM ".$array2[$y-1]);
		while($rows=mysql_fetch_row($cols))
		 {
		 if($array2[$y-2]!=str_replace("_"," ",$rows[0])&&$array1[$x-1]!=str_replace("_"," ",$rows[0])&&$rows[0]!="Last_Updated_Date")
		 {
		 if($rows[0]=="School_Id"&&$array2[$y-1]!="School")
		 {
		 $res_que=mysql_query("SELECT * FROM school WHERE School_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($rows[0]=="Lab_Id"&&$array2[$y-1]!="lab")
		 {
		 $res_que=mysql_query("SELECT * FROM lab WHERE Lab_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Lab_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($rows[0]=="Resource_Id")
		 {
		 $res_que=mysql_query("SELECT * FROM resource WHERE Resource_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Resource_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($rows[0]=="Programme_Id")
		 {
		$res_que=mysql_query("SELECT * FROM programme WHERE Programme_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Programme_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($rows[0]=="Competition_Id")
		 {		
		 $res_que=mysql_query("SELECT * FROM competitions WHERE Competition_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Competition_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else
		 {
		 $val=$row[$rows[0]];
		 }	 
		 echo '<td>'.str_replace("deg_","",$val).'</td>';         		 
		 }
		 }
		 echo '</tr>';
		 }	
		  echo '</table><br/><br/>';
	  }
if($_GET['action']=="triple")
	  {
	  echo '<table border="1" class="tablefontv">';
	echo '<tr bgcolor="#6AB5FF"><td>';
	     if($array1[$x-1]=="School Id"&&$array2[$y-1]!="school")
		 {
		 echo 'School Name';
		 }
		 else if($array1[$x-1]=="Lab Id"&&$array2[$y-1]!="lab")
		 {
		 echo 'Lab Type';
		 }
		 else if($array1[$x-1]=="Resource Id")
		 {
		 echo 'Resource Type';
		 }
		 else if($array1[$x-1]=="Programme Id")
		 {
		 echo 'Programme Name';
		 }
		 else if($array1[$x-1]=="Competition Id")
		 {
		 echo 'Competition Name';
		 }
		 else
		 {
	      echo $array1[$x-1];
	     }
	echo '</td><td>';
	    if($array2[$y-2]=="School Id"&&$array2[$y-1]!="school")
		 {
		 echo 'School Name';
		 }
		 else if($array2[$y-2]=="Lab Id"&&$array2[$y-1]!="lab")
		 {
		 echo 'Lab Type';
		 }
		 else if($array2[$y-2]=="Resource Id")
		 {
		 echo 'Resource Type';
		 }
		 else if($array2[$y-2]=="Programme Id")
		 {
		 echo 'Programme Name';
		 }
		 else if($array2[$y-2]=="Competition Id")
		 {
		 echo 'Competition Name';
		 }
		 else
		 {
	     echo $array2[$y-2];
	     }
	echo '</td><td>';
	    if($array3[$z-1]=="School Id"&&$array2[$y-1]!="school")
		 {
		 echo 'School Name';
		 }
		 else if($array2[$z-1]=="Lab Id"&&$array2[$y-1]!="lab")
		 {
		 echo 'Lab Type';
		 }
		 else if($array3[$z-1]=="Resource Id")
		 {
		 echo 'Resource Type';
		 }
		 else if($array3[$z-1]=="Programme Id")
		 {
		 echo 'Programme Name';
		 }
		 else if($array3[$z-1]=="Competition Id")
		 {
		 echo 'Competition Name';
		 }
		 else
		 {
	     echo $array3[$z-1];
	     }
	echo '</td>';
	$cols=mysql_query("SHOW COLUMNS FROM ".$array2[$y-1]);
	while($rows=mysql_fetch_row($cols))
		 {
		 if($array2[$y-2]!=str_replace("_"," ",$rows[0])&&$array1[$x-1]!=str_replace("_"," ",$rows[0])&&$array3[$z-1]!=str_replace("_"," ",$rows[0])&&$rows[0]!="Last_Updated_Date")
		 {
		 if($rows[0]=="School_Id"&&$array2[$y-1]!="school")
		 {
		 echo '<td>School Name</td>';
		 }
		 else if($rows[0]=="Lab_Id"&&$array2[$y-1]!="lab")
		 {
		 echo '<td>Lab Type</td>';
		 }
		 else if($rows[0]=="Resource_Id")
		 {
		 echo '<td>Resource Type</td>';
		 }
		 else if($rows[0]=="Programme_Id")
		 {
		 echo '<td>Programme Name</td>';
		 }
		 else if($rows[0]=="Competition_Id")
		 {
		 echo '<td>Competition Name</td>';
		 }
		 else
		 {
		 echo '<td>'.str_replace("_"," ",$rows[0]).'</td>';
		 }
		 }
		 }
		 echo '</tr>';
		 
		 $que=mysql_query($Q);
		  while($row=mysql_fetch_array($que))
		 {
		 echo '<tr bgcolor="#999999"><td>';
		 if($array1[$x-1]=="School Id"&&$array2[$y-1]!="School")
		 {
		 $res_que=mysql_query("SELECT * FROM school WHERE School_Id='".$row['School_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array1[$x-1]=="Lab Id"&&$array2[$y-1]!="lab")
		 {		 
		 $res_que=mysql_query("SELECT * FROM lab WHERE Lab_Id='".$row['Lab_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		$val=$row_res['Lab_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array1[$x-1]=="Resource Id")
		 {
		 $res_que=mysql_query("SELECT * FROM resource WHERE Resource_Id='".$row['Resource_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Resource_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array1[$x-1]=="Programme Id")
		 {
		$res_que=mysql_query("SELECT * FROM programme WHERE Programme_Id='".$row['Programme_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Programme_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array1[$x-1]=="Competition Id")
		 {		
		 $res_que=mysql_query("SELECT * FROM competitions WHERE Competition_Id='".$row['Competition_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Competition_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else
		 {
		 $col=str_replace(" ","_",$array1[$x-1]);
		 $val=$row[$col];
		 }
		
		 echo $val;
		 echo '</td><td>';
		 
		 if($array2[$y-2]=="School Id"&&$array2[$y-1]!="School")
		 {
		 $res_que=mysql_query("SELECT * FROM school WHERE School_Id='".$row['School_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array2[$y-2]=="Lab Id"&&$array2[$y-1]!="lab")
		 {
		 $res_que=mysql_query("SELECT * FROM lab WHERE Lab_Id='".$row['Lab_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Lab_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array2[$y-2]=="Resource Id")
		 {
		 $res_que=mysql_query("SELECT * FROM resource WHERE Resource_Id='".$row['Resource_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Resource_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array2[$y-2]=="Programme Id")
		 {
		$res_que=mysql_query("SELECT * FROM programme WHERE Programme_Id='".$row['Programme_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Programme_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array2[$y-2]=="Competition Id")
		 {		
		 $res_que=mysql_query("SELECT * FROM competitions WHERE Competition_Id='".$row['Competition_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Competition_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else
		 {
		 $col=str_replace(" ","_",$array2[$y-2]);
		 $val=$row[$col];
		 }
		 
		echo str_replace("deg_","",$val);
		 
		 echo '</td><td>';
		 
		 if($array3[$z-1]=="School Id"&&$array2[$y-1]!="School")
		 {
		 $res_que=mysql_query("SELECT * FROM school WHERE School_Id='".$row['School_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array3[$z-1]=="Lab Id"&&$array2[$y-1]!="lab")
		 {
		 $res_que=mysql_query("SELECT * FROM lab WHERE Lab_Id='".$row['Lab_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Lab_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array3[$z-1]=="Resource Id")
		 {
		 $res_que=mysql_query("SELECT * FROM resource WHERE Resource_Id='".$row['Resource_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Resource_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array3[$z-1]=="Programme Id")
		 {
		$res_que=mysql_query("SELECT * FROM programme WHERE Programme_Id='".$row['Programme_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Programme_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($array3[$z-1]=="Competition Id")
		 {		
		 $res_que=mysql_query("SELECT * FROM competitions WHERE Competition_Id='".$row['Competition_Id']."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Competition_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else
		 {
		 $col=str_replace(" ","_",$array3[$z-1]);
		 $val=$row[$col];
		 }
		 
		echo str_replace("deg_","",$val);
		 
		 echo '</td>';
		 
		$cols=mysql_query("SHOW COLUMNS FROM ".$array2[$y-1]);
		while($rows=mysql_fetch_row($cols))
		 {
		 if($array2[$y-2]!=str_replace("_"," ",$rows[0])&&$array1[$x-1]!=str_replace("_"," ",$rows[0])&&$array3[$z-1]!=str_replace("_"," ",$rows[0])&&$rows[0]!="Last_Updated_Date")
		 {
		 if($rows[0]=="School_Id"&&$array2[$y-1]!="School")
		 {
		 $res_que=mysql_query("SELECT * FROM school WHERE School_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($rows[0]=="Lab_Id"&&$array2[$y-1]!="lab")
		 {
		 $res_que=mysql_query("SELECT * FROM lab WHERE Lab_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Lab_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($rows[0]=="Resource_Id")
		 {
		 $res_que=mysql_query("SELECT * FROM resource WHERE Resource_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Resource_Type'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($rows[0]=="Programme_Id")
		 {
		$res_que=mysql_query("SELECT * FROM programme WHERE Programme_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Programme_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else if($rows[0]=="Competition_Id")
		 {		
		 $res_que=mysql_query("SELECT * FROM competitions WHERE Competition_Id='".$row[$rows[0]]."'");
		 if($row_res=mysql_fetch_array($res_que))
		 {
		 $val=$row_res['Competition_Name'];
		 }
		 else
		 {
		 $val="";
		 }
		 }
		 else
		 {
		 $val=$row[$rows[0]];
		 }
		 
		 echo '<td>'.str_replace("deg_","",$val).'</td>';
         		 
		 }
		 }
		 echo '</tr>';
		 }	
		  echo '</table><br/><br/>';
	  }
	  if($table=="teacher"||$table=="lab"||$table=="school")
	  {
	  echo '</div>';
	  }
	  else
	  {
	  if(mysql_num_rows($que)>10)
	  {
	  echo '</div>';
	  }
	  }
	  $Exc_quer=str_replace("'","@",$Q);
 echo "<form action='exportreport.php' method='post'><input type='submit' name='ToExcel' class='print' value='' style='background-image:url(images/b_print.png); width:100;'/><input type='hidden' value='$Exc_quer' name='query'/><input type='hidden' value='$table' name='Exc_table_name'/></form>";
}
}
?>
